package com.idbk.chargestation.activity.assist;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.dtr.zxing.utils.QRCodeUtil;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationURL;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.dialog.DialogShare;

import java.io.File;

/**
 * APP二维码下载
 * @author liangzw,lupc,zhx
 *
 */
public class ActivityQrCode extends BaseActivity implements OnClickListener{

	public final static String QR_FOR_SERVER_FILE_NAME = "qr_for_server.jpg";
	public final static String QR_CODE = ChargeStationURL.getRootURL() + "/appinfos.do?sn=0";
		
	private ImageView mImageView;//二维码显示
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_qr_code);
		
		setupToolBar2();
		mToolbar2.getImageMenu2().setVisibility(View.VISIBLE);
		mToolbar2.getImageMenu2().setImageResource(R.drawable.btn_map_share_normal);
		mToolbar2.getImageMenu2().setOnClickListener(this);
		
		mImageView = (ImageView) findViewById(R.id.qr_code_image);
		createQRCode();
	}
	
	private void createQRCode() {
		final String filePath = getFileRoot(ActivityQrCode.this) + File.separator
				+ QR_FOR_SERVER_FILE_NAME;
		QRCodeUtil.createQRImage(
				QR_CODE, 800, 800, 
				BitmapFactory.decodeResource(getResources(), R.drawable.logo),
				filePath);
		mImageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
	}
	
	//文件存储目录
	private String getFileRoot(Context context) {
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			File external = context.getExternalFilesDir(null);
			if(external != null) {
				return external.getAbsolutePath();
			}
		}
		return context.getFilesDir().getAbsolutePath();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageview_menu_2:
			new DialogShare(ActivityQrCode.this, QR_CODE, getString(R.string.app_name))
			.show();
			break;
		default:
			break;
		}
	}

}
